package org.loofer.ext.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import org.loofer.ext.component.logger.Logger;
import java.io.File;
import java.lang.reflect.Method;

public final class ApkUtils {
    private static Class<AssetManager> CLASS_ASSET = AssetManager.class;
    private static Method METHOD_ADD_ASSET = null;
    private static final String TAG = "ApkUtils";

    public static class ApkInfo {
        public Drawable icon;
        public String name;
        public PackageInfo packageInfo;
        public String packageName;
        public float version;
        public String versionName;
    }

    static {
        try {
            METHOD_ADD_ASSET = CLASS_ASSET.getDeclaredMethod("addAssetPath", new Class[]{String.class});
        } catch (Throwable e) {
            Logger.w(TAG, e);
        } catch (Throwable e2) {
            Logger.w(TAG, e2);
        }
    }

    private ApkUtils() {
    }

    public static Resources getResources(Context context, String apkPath) {
        if (!checkApkFile(apkPath)) {
            return null;
        }
        Resources resources = null;
        PackageInfo pkgInfo = context.getPackageManager().getPackageArchiveInfo(apkPath, 0);
        ApplicationInfo appInfo = pkgInfo == null ? null : pkgInfo.applicationInfo;
        if (appInfo != null) {
            appInfo.sourceDir = apkPath;
            appInfo.publicSourceDir = apkPath;
            try {
                resources = context.getPackageManager().getResourcesForApplication(appInfo);
            } catch (Throwable e) {
                Logger.w(TAG, e);
            } catch (Throwable e2) {
                Logger.w(TAG, e2);
            }
        }
        if (resources == null) {
            return getResourcesWithReflect(context, apkPath);
        }
        return resources;
    }

    private static Resources getResourcesWithReflect(Context context, String apkPath) {
        if (CLASS_ASSET == null || METHOD_ADD_ASSET == null || !checkApkFile(apkPath)) {
            return null;
        }
        try {
            AssetManager asset = (AssetManager) CLASS_ASSET.newInstance();
            METHOD_ADD_ASSET.invoke(asset, new Object[]{apkPath});
            return new Resources(asset, context.getResources().getDisplayMetrics(), context.getResources().getConfiguration());
        } catch (Throwable e) {
            Logger.w(TAG, e);
            return null;
        } catch (Throwable e2) {
            Logger.w(TAG, e2);
            return null;
        } catch (Throwable e22) {
            Logger.w(TAG, e22);
            return null;
        } catch (Throwable e222) {
            Logger.w(TAG, e222);
            return null;
        }
    }

    public static boolean addResourcesExtra(Resources resources, String path) {
        if (resources == null || !checkApkFile(path) || METHOD_ADD_ASSET == null) {
            return false;
        }
        try {
            METHOD_ADD_ASSET.invoke(resources.getAssets(), new Object[]{path});
            return true;
        } catch (Throwable e) {
            Logger.w(TAG, e);
            return false;
        } catch (Throwable e2) {
            Logger.w(TAG, e2);
            return false;
        }
    }

    public static PackageInfo getPackageInfo(Context context, String apkPath) {
        return getPackageInfo(context, apkPath, 0);
    }

    public static PackageInfo getPackageInfo(Context context, String apkPath, int flags) {
        if (!checkApkFile(apkPath)) {
            return null;
        }
        PackageInfo pkgInfo = context.getPackageManager().getPackageArchiveInfo(apkPath, flags);
        if (pkgInfo == null) {
            return null;
        }
        if ((flags & 64) == 0 || pkgInfo.signatures != null) {
            return pkgInfo;
        }
        pkgInfo.signatures = CertificatesUtils.collectCertificates(apkPath);
        return pkgInfo;
    }

    public static ApplicationInfo getApplicationInfo(Context context, String apkPath) {
        return getApplicationInfo(context, apkPath, 0);
    }

    public static ApplicationInfo getApplicationInfo(Context context, String apkPath, int flags) {
        ApplicationInfo appInfo = null;
        if (checkApkFile(apkPath)) {
            PackageInfo pkgInfo = context.getPackageManager().getPackageArchiveInfo(apkPath, flags);
            if (pkgInfo != null) {
                appInfo = pkgInfo.applicationInfo;
            }
            if (appInfo != null) {
                appInfo.sourceDir = apkPath;
                appInfo.publicSourceDir = apkPath;
            }
        }
        return appInfo;
    }

    public static ApkInfo getApkInfo(Context context, String apkPath) {
        Throwable e;
        if (!checkApkFile(apkPath)) {
            return null;
        }
        ApkInfo apkInfo = null;
        try {
            PackageInfo pkgInfo = context.getPackageManager().getPackageArchiveInfo(apkPath, 0);
            Resources res = getResources(context, apkPath);
            if (pkgInfo == null || res == null) {
                return null;
            }
            Drawable icon;
            ApplicationInfo appInfo = pkgInfo.applicationInfo;
            String name = appInfo == null ? null : getString(res, appInfo.labelRes);
            if (appInfo == null) {
                icon = null;
            } else {
                icon = getDrawable(res, appInfo.icon);
            }
            if (icon == null && appInfo != null) {
                icon = context.getPackageManager().getApplicationIcon(appInfo);
            }
            ApkInfo apkInfo2 = new ApkInfo();
            try {
                apkInfo2.packageInfo = pkgInfo;
                apkInfo2.packageName = pkgInfo.packageName;
                apkInfo2.name = name;
                apkInfo2.icon = icon;
                apkInfo2.version = (float) pkgInfo.versionCode;
                apkInfo2.versionName = pkgInfo.versionName;
                return apkInfo2;
            } catch (Throwable th) {
                e = th;
                apkInfo = apkInfo2;
                Logger.w(TAG, e);
                return apkInfo;
            }
        } catch (Throwable th2) {
            e = th2;
            Logger.w(TAG, e);
            return apkInfo;
        }
    }

    private static Drawable getDrawable(Resources resources, int id) {
        try {
            return resources.getDrawable(id);
        } catch (NotFoundException e) {
            return null;
        }
    }

    private static String getString(Resources resources, int id) {
        try {
            return resources.getString(id);
        } catch (NotFoundException e) {
            return null;
        }
    }

    private static boolean checkApkFile(String apkPath) {
        if (apkPath == null || apkPath.length() == 0) {
            return false;
        }
        File apkFile = new File(apkPath);
        if (apkFile.exists() && apkFile.isFile()) {
            return true;
        }
        return false;
    }
}
